package com.rul.mars.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.rul.mars.mbg.domain.CollectionDetail;
import com.rul.mars.mbg.domain.ProductDetail;
import com.rul.mars.mbg.mapper.CollectionMapper;
import com.rul.mars.mbg.mapper.ProductMapper;
import com.rul.mars.mbg.model.*;
import com.rul.mars.product.service.ProductCollectionService;
import com.rul.mars.product.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品收藏服务接口实现
 *
 * @author RuL
 */
@Service
public class ProductCollectionServiceImpl implements ProductCollectionService {

    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UserService userService;

    /**
     * 添加商品收藏
     *
     * @param productId 商品ID
     * @return 收藏数
     */
    @Override
    public int collectProduct(Long productId) {
        User user = userService.currentUser();
        Collection collection = new Collection();

        collection.setUserId(user.getId());
        collection.setProductId(productId);
        return collectionMapper.insertSelective(collection);
    }

    /**
     * 查询用户收藏的所有商品详情
     *
     * @return 用户所有商品收藏详情
     */
    @Override
    public List<CollectionDetail> listCollection() {
        User user = userService.currentUser();
        CollectionExample collectionExample = new CollectionExample();
        collectionExample.createCriteria()
                .andUserIdEqualTo(user.getId());
        //用户收藏的商品信息
        List<Collection> collectionList = collectionMapper.selectByExample(collectionExample);

        //商品ID
        List<Long> productIds = collectionList.stream()
                .map(Collection::getProductId)
                .collect(Collectors.toList());
        ProductExample productExample = new ProductExample();
        productExample.createCriteria()
                .andIdIn(productIds);
        //商品信息
        List<Product> productList = productMapper.selectByExample(productExample);

        //转为收藏详情信息并返回
        return collectionList.stream()
                .map(item -> BeanUtil.copyProperties(item, CollectionDetail.class))
                .map(item -> covertProductDetail(item, productList))
                .collect(Collectors.toList());
    }

    /**
     * 为收藏详情设置商品详情
     *
     * @param collectionDetail 收藏详情
     * @param productList      商品
     * @return 收藏详情
     */
    public CollectionDetail covertProductDetail(CollectionDetail collectionDetail, List<Product> productList) {
        List<Product> products = productList.stream()
                .filter(item -> item.getId().equals(collectionDetail.getProductId()))
                .collect(Collectors.toList());
        //设置商品信息
        if (CollUtil.isNotEmpty(products)) {
            ProductDetail productDetail = BeanUtil.copyProperties(products.get(0), ProductDetail.class);
            collectionDetail.setProductDetail(productDetail);
        }
        return collectionDetail;
    }
}
